package com.htsc.mrp.service.Impl;

import com.htsc.mrp.dao.IGeneratorDao;
import com.htsc.mrp.po.Generator;
import com.htsc.mrp.po.GeneratorInit;
import com.htsc.mrp.service.IGeneratorService;
import com.htsc.mrp.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
@Component
@PropertySource("classpath:dev.properties")
public class GeneratorService implements IGeneratorService {

    @Autowired
    private IGeneratorDao generatorDao;

    @Value("${mrp.ftp.host}")
    private String ftpHost;

    @Value("${mrp.ftp.username}")
    private String ftpUserName;

    @Value("${mrp.ftp.password}")
    private String ftpPassword;

    @Value("${mrp.ftp.port}")
    private int ftpPort;

    @Value("${mrp.ftp.path}")
    private String ftpPath;


    @Override
    public void createOrUpdateFile(GeneratorInit generatorInit) {

        ExecutorService executor = Executors.newSingleThreadExecutor();
        //处理字符串
        String targetProject = StringUtil.handleTargetProject(generatorInit.getDaoAddress());
        String targetResources = StringUtil.handleTargetResources(generatorInit.getMapperAddress());
        String modelTargetPackage = StringUtil.handleModelTargetPackage(generatorInit.getPoAddress());
        String daoTargetPackage = StringUtil.handleDaoTargetPackage(generatorInit.getDaoAddress());
        String mapperTargetPackage = StringUtil.handleMapperTargetPackage(generatorInit.getMapperAddress());
        String jdbcUrl = StringUtil.handleJdbcUrl(generatorInit.getDbIpPort());
        try {
            //修改generator.xml文件
            /*executor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行修改generatorConfig.xml文件操作");
                    try {*/
                        UpdateXmlUtil.updateGeneratorXML(
                                generatorInit.getDbName(),
                                generatorInit.getDbPassword(),
                                generatorInit.getDbTableName(),
                                targetProject,
                                targetResources,
                                modelTargetPackage,
                                daoTargetPackage,
                                mapperTargetPackage,
                                jdbcUrl
                        );
                    /*} catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });*/

            //启动mybatis-generator生成mapper，po，dao文件
            /*executor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行生成文件操作");
                    try {*/
                        GeneratorSqlmap.generator();

                   /* } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });*/
            //生成dao接口的实现类
            /*executor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行生成接口实现类操作");
                    try {*/
                        CreateDaoImplUtil.createDaoImpl(
                                daoTargetPackage,
                                modelTargetPackage,
                                generatorInit.getDbTableName(),
                                generatorInit.getDaoAddress()
                        );
                    /*} catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });*/
            //修改mapper.xml文件
            /*executor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行mapper文件修改操作");
                    try {*/
                        UpdateXmlUtil.updateMapperXML(generatorInit.getMapperAddress(), generatorInit.getDbTableName());
                   /* } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });*/
            //上传文件到ftp
            /*executor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行上传文件到ftp操作");
                    try {
                        FtpUtil.batchUploadFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, generatorInit);
                    } catch (Exception e) {
                        System.out.println("修改mapper文件出现异常");
                    }
                }
            });*/
        } catch (Exception e) {
            e.printStackTrace();
        }

        //记录数据存入数据库
        Generator generator = new Generator();
        //存po类文件信息
        generator.setFilename(StringUtil.lineToHump(generatorInit.getDbTableName())+".java");
        generator.setFilepath(generatorInit.getPoAddress()+"\\"+StringUtil.lineToHump(generatorInit.getDbTableName())+".java");
        generator.setCreateTime(new Date());
        //判断版本号，进行增量然后存入
        String poVersion = generatorDao.selectLastVersionByFileName(generator.getFilename());
        StringUtil.handleVersion(generator,poVersion);
        generatorDao.insert(generator);

        //存接口实现类文件信息
        generator.setFilename(StringUtil.lineToHump(generatorInit.getDbTableName())+"Dao.java");
        generator.setFilepath(generatorInit.getDaoAddress()+"\\impl\\"+StringUtil.lineToHump(generatorInit.getDbTableName())+"Dao.java");
        generator.setCreateTime(new Date());
        //判断版本号，进行增量然后存入
        String implVersion = generatorDao.selectLastVersionByFileName(generator.getFilename());
        StringUtil.handleVersion(generator,implVersion);
        generatorDao.insert(generator);

        //存dao接口文件信息
        generator.setFilename("I"+StringUtil.lineToHump(generatorInit.getDbTableName())+"Dao.java");
        generator.setFilepath(generatorInit.getDaoAddress()+"\\I"+StringUtil.lineToHump(generatorInit.getDbTableName())+"Dao.java");
        generator.setCreateTime(new Date());
        //判断版本号，进行增量然后存入
        String daoVersion = generatorDao.selectLastVersionByFileName(generator.getFilename());
        StringUtil.handleVersion(generator,daoVersion);
        generatorDao.insert(generator);

        //存mapper文件信息
        generator.setFilename(StringUtil.lineToHump(generatorInit.getDbTableName())+"Mapper.xml");
        generator.setFilepath(generatorInit.getMapperAddress()+"\\"+StringUtil.lineToHump(generatorInit.getDbTableName())+"Mapper.xml");
        generator.setCreateTime(new Date());
        //判断版本号，进行增量然后存入
        String napperVersion = generatorDao.selectLastVersionByFileName(generator.getFilename());
        StringUtil.handleVersion(generator,napperVersion);
        generatorDao.insert(generator);
    }

}
